var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "../services/local-service", "maishu-chitu-admin/static", "json!websiteConfig", "maishu-jueying"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    const local_service_1 = require("../services/local-service");
    const static_1 = require("maishu-chitu-admin/static");
    const json_websiteConfig_1 = require("json!websiteConfig");
    const maishu_jueying_1 = require("maishu-jueying");
    let localService = new local_service_1.LocalService(err => static_1.errorHandle);
    class ComponentLoader {
        /** 加载全部组件 */
        static loadAllComponents() {
            return __awaiter(this, void 0, void 0, function* () {
                let componentInfos = yield localService.componentInfos();
                let r = componentInfos.map(componentInfo => {
                    return new Promise((resolve, reject) => {
                        requirejs({ context: json_websiteConfig_1.contextName }, [componentInfo.path], (mod) => {
                            console.assert(componentInfo.name != null);
                            console.assert(typeof mod[componentInfo.name] == "function");
                            maishu_jueying_1.Component.register(componentInfo.name, mod[componentInfo.name]);
                            resolve();
                        }, () => {
                            reject();
                        });
                    });
                });
                return Promise.all(r);
            });
        }
        /** 加载页面的组件 */
        static loadPageCompontents(pageData) {
            return __awaiter(this, void 0, void 0, function* () {
                let componentInfos = yield localService.componentInfos();
                let componentTypes = pageData.children.map(o => o.type);
                let componentToLoad = componentInfos.filter(o => o.name != null && componentTypes.indexOf(o.name) >= 0);
                let r = componentToLoad.map(componentInfo => {
                    return new Promise((resolve, reject) => {
                        let componentPath = componentInfo.path;
                        requirejs({ context: json_websiteConfig_1.contextName }, [componentPath], (mod) => {
                            console.assert(componentInfo.name != null);
                            console.assert(typeof mod[componentInfo.name] == "function");
                            maishu_jueying_1.Component.register(componentInfo.name, mod[componentInfo.name]);
                            resolve();
                        }, () => {
                            reject();
                        });
                    });
                });
                return Promise.all(r);
            });
        }
        /** 加载单个组件 */
        static loadSingleComponent(componentData) {
            return __awaiter(this, void 0, void 0, function* () {
                let componentInfos = yield localService.componentInfos();
                let componentType = componentData.type;
                let componentToLoad = componentInfos.filter(o => o.name != null && componentType == o.name)[0];
                console.assert(componentToLoad != null);
                yield new Promise((resolve, reject) => {
                    let componentPath = componentToLoad.path;
                    requirejs({ context: json_websiteConfig_1.contextName }, [componentPath], (mod) => {
                        console.assert(componentToLoad.name != null);
                        console.assert(typeof mod[componentToLoad.name] == "function");
                        maishu_jueying_1.Component.register(componentToLoad.name, mod[componentToLoad.name]);
                        resolve();
                    }, () => {
                        reject();
                    });
                });
            });
        }
    }
    exports.ComponentLoader = ComponentLoader;
});
//======================================================================
// 加载全部组件
// export async function loadAllComponents(localService: LocalService) {
//     let componentInfos = await localService.componentInfos();
//     let r = componentInfos.map(componentInfo => {
//         return new Promise((resolve, reject) => {
//             let componentPath = componentInfo.path;
//             resolve();
//             requirejs({ context: contextName }, [componentPath],
//                 (mod) => {
//                     console.assert(componentInfo.name != null);
//                     console.assert(typeof mod[componentInfo.name] == "function");
//                     Component.register(componentInfo.name, mod[componentInfo.name]);
//                     // resolve();
//                 },
//                 () => {
//                     // reject();
//                 }
//             )
//         })
//     })
//     return Promise.all(r);
// }
//======================================================================
